<template>
  <el-container>
    <el-header>
      <div>
        <img src="../../assets/logo.png" />
        <span>商城管理系统</span>
      </div>
      <el-button type="primary" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggleCollapse" @click="toggleCollapse">|||</div>
        <el-menu class="el-menu-vertical-demo" background-color="#545c64" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
          <el-submenu :index="item.id + ''" :key="item.id" v-for="item in menuList">
            <template slot="title">
              <i :class="menuObject[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path" :key="subItem.id" v-for="subItem in item.children" @click="saveNavPath('/' + subItem.path)">
              <i class="el-icon-collection-tag"></i>
              <span> {{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { menuList } from "@/request/api";

@Component({
  components: {},
})
export default class Home extends Vue {
  isCollapse: boolean = false;
  activePath: string = "";
  menuList: Array<any> = [];
  menuObject: {
    "125": string;
    "103": string;
    "101": string;
    "102": string;
    "145": string;
  } = {
    "125": "el-icon-user",
    "103": "el-icon-key",
    "101": "el-icon-shopping-cart-2",
    "102": "el-icon-s-order",
    "145": "el-icon-s-data",
  };
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath") + "";
    // console.log(this.menuList);
  }
  // 获取菜单列表
  async getMenuList() {
    const { data } = await menuList();
    this.menuList = data;
    // console.log(data);
  }
  // 切换按钮展开隐藏菜单
  toggleCollapse() {
    this.isCollapse = !this.isCollapse;
  }
  // 退出登录
  logout() {
    this.$router.push("/login");
    window.sessionStorage.clear();
  }
  saveNavPath(activePath: string) {
    window.sessionStorage.setItem("activePath", activePath);
    this.activePath = activePath;
  }
}
</script>

<style lang="less" scoped>
.el-header {
  padding-left: 0px;
  background-color: skyblue;
  display: flex;
  justify-content: space-between;
  div {
    display: flex;
    width: 100%;
    height: 60px;
    line-height: 60px;
    img {
      height: 100%;
    }
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #545c64;
  > .el-menu {
    border-right: none;
  }
}
.el-container {
  height: 100%;
}
.toggleCollapse {
  background-color: #545c64;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  height: 24px;
  color: #fff;
  cursor: pointer;
}
</style>
